﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.IO.IsolatedStorage;
using QuickTasks;
using System.Data.Linq;
using System.Linq;
using Microsoft.Phone.Scheduler;

namespace BackAgent
{
    public class SetReminder
    {
        string PreviousLocation="0";
       
       
        string name = string.Empty;
       static string  remid=string.Empty;
       
        IsolatedStorageSettings settings = IsolatedStorageSettings.ApplicationSettings;
        QuickTaskDataContext context = new QuickTaskDataContext("isostore:/quick.sdf");
        public void AssignReminderBasedOnLocation()
        {
            var RetriveLoacations = from l in context.Loc select l;
            if (RetriveLoacations.Count() == 2)
            {
                foreach (var loc in RetriveLoacations)
                {
                    PreviousLocation = loc.id.ToString();
                    break;
                }
            }

            if (PreviousLocation != "0")
            {
                var RetriveReminder = from l in context.reminderalr where l.place == PreviousLocation select l;
                foreach (var rem in RetriveReminder)
                {
                    name = rem.Rguid;
                    remid = rem.id.ToString();
                    Uri navigationUri = new Uri("/Views/ToDoList/ToDoList.xaml?r=" + remid, UriKind.Relative);
                    Reminder reminder = new Reminder(name);
                    reminder.Title = rem.title;
                    reminder.Content = rem.description;
                    reminder.BeginTime = DateTime.Now.AddMinutes(1);
                    reminder.ExpirationTime = DateTime.Now.AddMinutes(2);
                    reminder.NavigationUri = navigationUri;
                    if (Convert.ToBoolean(ScheduledActionService.Find(name)))
                    {
                        ScheduledActionService.Remove(name);
                        ScheduledActionService.Add(reminder);
                    }
                    else
                    {
                        ScheduledActionService.Add(reminder);
                    }
                }
            }
        }
    }
}
